package com.iyundao.io.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.iyundao.io.Service.PersonalDetailsService;
import com.iyundao.io.Service.SysUserService;
import com.iyundao.io.Service.WishService;
import com.iyundao.io.model.*;
import com.iyundao.io.tools.GiftVo;
import com.iyundao.io.tools.IntegralOperationVo;
import com.iyundao.io.tools.PersonalDetailsShow;
import com.iyundao.io.tools.ResponseData;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/wish")
public class WishController {

    @Autowired
    private WishService wishService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private PersonalDetailsService personalDetailsService;

    @ApiOperation(value="心愿单添加", notes="心愿单添加")
    @RequestMapping(value = "/addwish", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResponseData addWish(@RequestBody String params) {
        SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        JSONObject object = JSONObject.parseObject(params);
        Integer id = (Integer) object.get("id");
        Wish wish = new Wish();
        wish.setGiftid(id);
        wish.setPartymemberid(sysUser.getSid());
        return wishService.insert(wish);

    }

    @ApiOperation(value="心愿单删除", notes="心愿单删除")
    @RequestMapping(value = "/delwish", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResponseData delWish(@RequestBody String params) {
        SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        JSONObject object = JSONObject.parseObject(params);
        Integer id = (Integer) object.get("id");
        Wish wish = new Wish();
        wish.setGiftid(id);
        wish.setPartymemberid(sysUser.getSid());
        return wishService.del(wish);

    }

    @ApiOperation(value="心愿单提交", notes="心愿单提交")
    @RequestMapping(value = "/wishsubmit", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResponseData wishSubmit(@RequestBody String params) {
        JSONObject object = JSONObject.parseObject(params);
        List<Integer> giftIds = (List<Integer>) object.get("giftIds");
        SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        List<Wish> list = new ArrayList<Wish>();
        for (Integer i: giftIds) {
            Wish wish = new Wish();
            wish.setPartymemberid(sysUser.getSid());
            wish.setGiftid(i);
            list.add(wish);
        }

        return wishService.addWishSubmit(list);

    }

    @ApiOperation(value="删除心愿提交单", notes="删除心愿提交单")
    @RequestMapping(value = "/delwishsubmit", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResponseData deleteWishSubmit(@RequestBody String params) {
        JSONObject object = JSONObject.parseObject(params);
        Integer id = (Integer) object.get("id");
        return wishService.deleteWishSubmit(id);
    }

    @ApiOperation(value="后台心愿单提交", notes="心愿单提交")
    @RequestMapping(value = "/wishsubmitcustom", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResponseData wishSubmitCustom(@RequestBody String params) {
        JSONObject object = JSONObject.parseObject(params);
        List<Integer> giftIds = (List<Integer>) object.get("giftIds");
        Integer id = (Integer) object.get("id");
        SysUser sysUser = sysUserService.selectByPartymemberId(id);
        List<Wish> list = new ArrayList<Wish>();
        for (Integer i: giftIds) {
            Wish wish = new Wish();
            wish.setPartymemberid(sysUser.getSid());
            wish.setGiftid(i);
            list.add(wish);
        }

        return wishService.addWishSubmit(list);

    }

    @ApiOperation(value="确认兑换", notes="确认兑换")
    @RequestMapping(value = "/exchange", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResponseData exchange(@RequestBody JSONObject params) {
        IntegralOperationVo integralOperationVo = JSON.toJavaObject(params, IntegralOperationVo.class);
        SysUserProvider sysUser = (SysUserProvider) SecurityUtils.getSubject().getPrincipal();
        integralOperationVo.setUserid(sysUser.getSid());
        return wishService.exchange(integralOperationVo);

    }

    @ApiOperation(value="通过提交人姓名模糊查询提交单", notes="通过提交人姓名模糊查询提交单")
    @RequestMapping(value = "/selectbylike", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResponseData selectByLike(@RequestBody String params) {
        JSONObject object = JSONObject.parseObject(params);
        String memberName = (String) object.get("partyMemberName");
        return wishService.selectByLikeName(memberName);
    }

    @ApiOperation(value="通过日期查询提交单", notes="通过日期查询提交单")
    @RequestMapping(value = "/selectbydate", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResponseData selectByDate(@RequestBody String params) {
        JSONObject object = JSONObject.parseObject(params);
        String date = (String) object.get("date");
        return wishService.selectByDate(date);
    }

    @ApiOperation(value="查询个人心愿单", notes="查询个人心愿单")
    @RequestMapping(value = "/selectbysid", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public PageInfo<GiftShow> selectBySid(@RequestBody String params) {
        JSONObject object = JSONObject.parseObject(params);
        Integer pageNum = (Integer) object.get("pageNum");
        Integer pagesize = (Integer) object.get("pageSize");
        return wishService.selectBySid(pageNum,pagesize);
    }

    @ApiOperation(value="查询本组织下的心愿提交单", notes="查询本组织下的心愿提交单")
    @RequestMapping(value = "/selectbydeptid", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public PageInfo<WishSubmitShow> selectByDeptId(@RequestBody String params) {
        JSONObject object = JSONObject.parseObject(params);
        Integer pageNum = (Integer) object.get("pageNum");
        Integer pageSize = (Integer) object.get("pageSize");
        return wishService.selectByDeptId(pageNum,pageSize);
    }

    @ApiOperation(value="根据提交单id查询详细信息", notes="根据提交单id查询详细信息")
    @RequestMapping(value = "/selectbysubmitid", produces = {"application/json;charset=UTF-8"})
    @CrossOrigin
    public ResponseData selectBySubmitId(@RequestBody String params) {
        JSONObject object = JSONObject.parseObject(params);
        Integer pageNum = (Integer) object.get("pageNum");
        Integer pageSize = (Integer) object.get("pageSize");
        Integer id = (Integer) object.get("id");
        return wishService.selectWishSubmitDetailBySubmitId(pageNum, pageSize, id);
    }

}
